package cc.yaomm.service.basic.controller.business.login.biz;

import cc.yaomm.common.basic.captcha.CaptchaBusiness;
import cc.yaomm.common.basic.enums.CaptchaType;
import cc.yaomm.common.basic.util.BeanUtils;
import cc.yaomm.common.biz.constant.RedisConstant;
import cc.yaomm.common.biz.enums.SystemEnum;
import cc.yaomm.common.biz.enums.LoginTypeEnum;
import cc.yaomm.service.basic.controller.business.BaseAdminBiz;
import cc.yaomm.service.basic.controller.business.login.vo.LoginParamVO;
import cc.yaomm.service.basic.controller.business.login.vo.LoginResultVO;
import cc.yaomm.service.basic.service.user.LoginService;
import cc.yaomm.service.basic.service.user.bo.LoginParamBO;
import cc.yaomm.service.basic.service.user.bo.LoginResultBO;
import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * Created by XQL on 2021/6/16.
 */
@Service
public class LoginAdminBiz extends BaseAdminBiz {

    @Autowired
    private LoginService loginService;

    @Autowired
    private CaptchaBusiness captchaBusiness;

    /**
     * <p>@Description : 登录验证 </p>
     * <p>@Author : QiLin.Xing </p>
     * <p>@Date : 2021/6/17 11:13 </p>
     */
    public LoginResultVO login(LoginParamVO paramVO) {
        String key = Joiner.on(RedisConstant.SEPARATOR).join(SystemEnum.BIZ, paramVO.getUuid());
        Assert.isTrue(captchaBusiness.verify(CaptchaType.IMAGE, key, paramVO.getCaptcha()), "登录失败，验证码错误");

        LoginParamBO paramBO = BeanUtils.map(paramVO, LoginParamBO.class);
        paramBO.setIp(super.getIP());
        paramBO.setLoginType(LoginTypeEnum.ACCOUNT);

        LoginResultBO resultBO = loginService.login(paramBO);
        LoginResultVO loginResultVO = BeanUtils.map(resultBO, LoginResultVO.class);
//
//        //处理顶级root
//        Set<RoutesResultVO> routes = Sets.newHashSet(
//                RoutesResultVO.builder()
//                        .router("root")
//                        .children(loginResultVO.getRouters()).build());
//        loginResultVO.setRouters(routes);
        return loginResultVO;
    }

}
